package com.abocode.aichat.configuration;

import com.abocode.aichat.server.channel.ProtocolChannelRegistryImpl;
import com.abocode.aichat.server.context.ServerRuntimeContext;
import com.abocode.aichat.server.delivery.inbound.InboundServerDeliver;
import com.abocode.aichat.server.delivery.outbound.OutboundServerDeliver;
import com.abocode.aichat.server.pipeline.ChannelOnlinePipeline;
import com.abocode.aichat.server.pipeline.MessageGatewayPipeline;
import com.abocode.aichat.server.worker.ProtocolStanzaWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class AppMessengerConfiguration {
    @Autowired
    private ChannelOnlinePipeline channelOnlinePipeline;
    @Autowired
    private MessageGatewayPipeline messageGatewayPipeline;

    @Bean
    public ProtocolChannelRegistryImpl protocolChannelRegistryImpl() {
        ProtocolChannelRegistryImpl protocolChannelRegistry = new ProtocolChannelRegistryImpl();
        protocolChannelRegistry.setChannelOnlinePipeline(channelOnlinePipeline);
        return protocolChannelRegistry;
    }

    @Bean
    public OutboundServerDeliver outboundServerDeliver() {
        return new OutboundServerDeliver();
    }

    @Bean
    public InboundServerDeliver inboundServerDeliver() {
        return new InboundServerDeliver();
    }

    @Bean
    public ProtocolStanzaWorker protocolStanzaWorker() {
        return new ProtocolStanzaWorker();
    }

    @Bean
    public ServerRuntimeContext serverRuntimeContext() {
        ServerRuntimeContext serverRuntimeContext = new ServerRuntimeContext();
        serverRuntimeContext.setInboundServerDeliver(inboundServerDeliver());
        serverRuntimeContext.setProtocolChannelRegistry(protocolChannelRegistryImpl());
        serverRuntimeContext.setMessageGatewayPipeline(messageGatewayPipeline);
        serverRuntimeContext.setOutboundServerDeliver(outboundServerDeliver());
        serverRuntimeContext.setWorker(protocolStanzaWorker());
//        serverRuntimeContext.redisEndpointRegistryImpl
        return serverRuntimeContext;
    }

    @Bean
    public StringRedisSerializer stringRedisSerializer(){
        return  new StringRedisSerializer();
    }
    @Bean
    public MessageListenerAdapter messageListener(){
        MessageListenerAdapter   messageListener=new MessageListenerAdapter();
        messageListener.setSerializer(stringRedisSerializer());
        messageListener.setDelegate(outboundServerDeliver());
//        messageListener.setDefaultListenerMethod(handleMessage());
        return  messageListener;
    }

    /***
     * redis监听器
     * @return
     */
    @Autowired
   private RedisConnectionFactory redisConnectionFactory;
    @Bean
    public RedisMessageListenerContainer redisMessageListenerContainer(){
        RedisMessageListenerContainer messageListenerContainer=new RedisMessageListenerContainer();
        messageListenerContainer.setConnectionFactory(redisConnectionFactory);
      /*  Map messageListeners=new HashMap();
        messageListeners.put(messageListener(),Arrays.asList(channelTopic()));
        messageListenerContainer.setMessageListeners(messageListeners);*/
        return  messageListenerContainer;
    }

    @Value("${im.domain}")
    private  String domain;
    @Bean
    public ChannelTopic  channelTopic(){
        ChannelTopic channelTopic=new ChannelTopic(domain);
        return channelTopic;
    }

}
